package com.jintian.smart.kernel.flowable.component.action.process;

import java.util.List;
import java.util.concurrent.Callable;

import com.jintian.smart.kernel.core.flow.FlowAction;
import com.jintian.smart.kernel.core.flow.ProcessControl;
import com.jintian.smart.kernel.flowable.component.action.base.AbstractProcessAction;
import com.jintian.smart.kernel.flowable.context.DefaultProcessContext;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;


@Component
public class AbortProcessAction extends AbstractProcessAction<Void> {
    @Override
    public FlowAction getAction() {
        return FlowAction.abort;
    }

    @Override
    protected Void innerExecute(TaskEntity task, ProcessControl processControl) throws Exception {
        if (task.isSuspended()) {
            throw new IllegalArgumentException("流程挂起中");
        }
        ProcessInstance pi = this.getProcessInstance(task.getProcessInstanceId());
        try (DefaultProcessContext context = (DefaultProcessContext) this.processContextProvider.createInstance(
                pi.getProcessDefinitionKey(), task.getTaskDefinitionKey(), this.getAction(),
                task.getProcessInstanceId(), task.getId(), pi.getBusinessKey(), processControl);) {
            this.beforeAction(task, context, this.getAction());
            AbortProcessAction self = this;
            this.invokeAction(context, new Callable<List<TaskInfo>>() {
                @Override
                public List<TaskInfo> call() throws Exception {
                    // 作废数据
                    self.abortTask((TaskEntity) task, processControl);
                    return null;
                }
            });
        }
        return null;
    }
}
